
cap log close
log using "$logs/seda-gaps-sensitivity.log", replace
	

/********************************************************************************
	SENSITIVITY ONE: EXPLORING WHAT HAPPENS WITHOUT SAMPLE RESTRICTIONS
********************************************************************/
	
	use "$data/sedagaps", clear	
	
	// Merge with covariates
	merge 1:1 countyid using "$data/seda_covariates_clean", nogen
	
	// Merge with bias
	merge 1:1 countyid using "$data/mrp_data", nogen 

	// Standardize bias scores
	foreach var in all_imp_bias all_exp_bias tch_imp_bias tch_exp_bias {
		egen temp = std(`var')
		replace `var' = temp
		drop temp
		}
		
	// Generate a state variable
	tab fips, gen(state_)
	
	est clear
	
	// Pooled
	eststo: metareg mn_avg_ol all_imp_bias state_*, wsse(mn_avg_ol_se) mm
	eststo: metareg mn_avg_ol all_imp_bias ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
		state_*, wsse(mn_avg_ol_se) mm // All
		
	eststo: metareg mn_avg_ol all_exp_bias state_*, wsse(mn_avg_ol_se) mm
	eststo: metareg mn_avg_ol all_exp_bias ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
		state_*, wsse(mn_avg_ol_se) mm // All
	
	eststo: metareg mn_avg_ol tch_imp_bias state_*, wsse(mn_avg_ol_se) mm
	eststo: metareg mn_avg_ol tch_imp_bias ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
		state_*, wsse(mn_avg_ol_se) mm // All
		
	eststo: metareg mn_avg_ol tch_exp_bias state_*, wsse(mn_avg_ol_se) mm
	eststo: metareg mn_avg_ol tch_exp_bias ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
		state_*, wsse(mn_avg_ol_se) mm // All		
		
	esttab using "$output/seda-gaps-sensitivity-fullsample.csv", se r2 ar2 keep(???_???_bias) title(pooled bias) replace note(includes state FE) ///
		star(~ 0.10 * 0.05 ** 0.01 *** 0.001)
	
	est clear

/********************************************************************************
	SENSITIVITY TWO: CLUSTERING
********************************************************************/
	
use "$data/sedagaps", clear

	// Merge with sample 
	merge 1:1 countyid using "$data/commonsamp" 
	tab _merge // 91% of counties
	keep if _merge == 3
	drop _merge
	
	// Merge with covariates
	merge 1:1 countyid using "$data/seda_covariates_clean", assert(2 3) nogen keep(3)
	
	// Merge with bias
	merge 1:1 countyid using "$data/mrp_data", assert(2 3) nogen keep(3)
		
	preserve

		// Standardize bias scores
		foreach var in all_imp_bias all_exp_bias tch_imp_bias tch_exp_bias {
			egen temp = std(`var')
			replace `var' = temp
			drop temp
			}
			
		// Generate a state variable
		tab fips, gen(state_)
		
		est clear

		// Pooled
		eststo: reg mn_avg_ol all_imp_bias state_*
		eststo: reg mn_avg_ol all_imp_bias ///
			sesall ///
			perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
			ppexp_inst stutch_all ///
			flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
			state_*
		eststo: reg mn_avg_ol all_imp_bias state_*, vce(cluster fips)
		eststo: reg mn_avg_ol all_imp_bias ///
			sesall ///
			perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
			ppexp_inst stutch_all ///
			flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
			state_*, vce(cluster fips)
			
		eststo: reg mn_avg_ol all_exp_bias state_*
		eststo: reg mn_avg_ol all_exp_bias ///
			sesall ///
			perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
			ppexp_inst stutch_all ///
			flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
			state_*
		eststo: reg mn_avg_ol all_exp_bias state_*, vce(cluster fips)
		eststo: reg mn_avg_ol all_exp_bias ///
			sesall ///
			perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
			ppexp_inst stutch_all ///
			flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
			state_*, vce(cluster fips)
			
	restore

	// Teachers

		keep if tchsamp

		// Standardize bias scores
			foreach var in all_imp_bias all_exp_bias tch_imp_bias tch_exp_bias {
				egen temp = std(`var')
				replace `var' = temp
				drop temp
				}
			
		// Generate a state variable
		tab fips, gen(state_)
		
		// Pooled
		eststo: reg mn_avg_ol tch_imp_bias state_*
		eststo: reg mn_avg_ol tch_imp_bias ///
			sesall ///
			perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
			ppexp_inst stutch_all ///
			flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
			state_*

		eststo: reg mn_avg_ol tch_imp_bias state_*, vce(cluster fips)
		eststo: reg mn_avg_ol tch_imp_bias ///
			sesall ///
			perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
			ppexp_inst stutch_all ///
			flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
			state_*, vce(cluster fips)


		eststo: reg mn_avg_ol tch_exp_bias state_*
		eststo: reg mn_avg_ol tch_exp_bias ///
			sesall ///
			perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
			ppexp_inst stutch_all ///
			flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
			state_*
	
		eststo: reg mn_avg_ol tch_exp_bias state_*, vce(cluster fips)
		eststo: reg mn_avg_ol tch_exp_bias ///
			sesall ///
			perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
			ppexp_inst stutch_all ///
			flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
			state_*, vce(cluster fips)

	esttab using "$output/seda-gaps-sensitivity-cluster.csv", se r2 ar2 keep(???_???_bias) title(pooled bias) replace note(includes state FE) ///
		star(~ 0.10 * 0.05 ** 0.01 *** 0.001)

		
log close
